<?php
class BlogAction extends ComAction{
    
    public function Index(){
       $type=(int) $_GET['type'];
       
     //p($blog);die();
       import('ORG.Util.Page');
       $count=D('BlogRelation')->relation(true)->where(array('del'=>$type))->count();
       $page=new Page($count,10);
       $limit=$page->firstRow.','.$page->listRows;
       $this->blog=D('BlogRelation')->limit($limit)->GetBlog($type);
       $this->page=$page->show();
       $this->display();
    }
    //回收站
    public function ToRecycle(){
     $id=(int) $_GET['id'];
     $type=$_GET['type'];
     $con=$type?'移到回收站':'还原';
     $date=array('id'=>$id,'del'=>$type);
     if(M('Blog')->where(array('id'=>$id))->save($date)){
              $this->success($con."成功");
     }else{
           $this->error($con."失败");
     }

    }

    public function Del(){
      $id=$_GET['id'];
      //$where=array('id'=>$id);
      if(M('Blog')->delete($id)){
           M('blog_attr')->where(array('bid'=>$id))->delete();
           $this->success("删除成功");
     }else{
           $this->error("删除失败");
     }
    }

    public function AddBlog(){
    	//博文分类
      $cate=M('cate')->order('sort')->select();
      import("class.Category",APP_PATH);
      $this->cate=Category::UnlimteForLevel($cate);
      
      //博文属性
      $this->attr=M('attr')->select();
    
    	$this->display();
    }
    
    public function AddBlogRun(){
     // p($_POST);die();
     
      $data=array(
         'title'=>$_POST['title'],
         //'content'=>I('content'),
         'content'=>$_POST['content'],
         'time'=>time(),
         'click'=> (int) $_POST['click'],
         'cid'=>(int)$_POST['cid'],
         'summary'=>$_POST['summary']
        );
    /* 
      if(isset($_POST['aid'])){
         foreach ($_POST['aid'] as $v) {
           $data['attr'][]=$v;
         }

      }
     

    //   p($data);die();
      //M('blog')->add($data);
     // D('BlogRelation')->relation(true)->add($data);
     D('BlogRelation')->relation(true)->add($data);
     //p($b);
    */
    
    if($bid=M('blog')->add($data)){
        //echo 11;die();
        if(isset($_POST['aid'])){
          $sql="insert into `blog_attr` (`bid`,`aid`) values";
          foreach ($_POST['aid'] as $v) {
            $sql.="(".$bid.",".$v."),";
          }
          $sql=rtrim($sql,",");
          //echo $sql;die();
          M('blog_attr')->query($sql);
          $this->success('添加成功');
         }
    }else{
         // echo "false";die();
           
           $this->error('添加失败');
         }

   }


//旧版ueditor上传方法
    public function Upload(){
       import('ORG.Net.UploadFile');
       $upload=new UploadFile();
       $upload->subType='date';
       $upload->autoSub=true;//当一个类里面没有autoSub属性或函数时，就会默认调用__SET()函数；
       $upload->dateFormat='Ym';
       $title=htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
        
      if($upload->upload('./Upload/')){
         $info=$upload->getUploadFileInfo();
        import("ORG.Util.Image");
       Image::water('./Upload/'.$info[0]["savename"],'./Data/water.png');

        echo json_encode(array(
            'url'=>$info[0]["savename"],
            'title'=>$title,
            'original'=>$info[0]["name"],
            'state'=>'SUCCESS'
          ));
      }else{
          echo json_encode(array(
            'state'=>$upload->getErrorMsg()
            ));
      }
       
      /* 向浏览器返回数据json数据
     * {
     *   'url'      :'a.jpg',   //保存后的文件路径
     *   'title'    :'hello',   //文件描述，对图片来说在前端会添加到title属性上
     *   'original' :'b.jpg',   //原始文件名
     *   'state'    :'SUCCESS'  //上传状态，成功时返回SUCCESS,其他任何值将原样返回至图片上传框中
     * }
     */
      }
//新版ueditor上传方法      
    public function Upload2(){
        date_default_timezone_set("Asia/chongqing");
        error_reporting(E_ERROR);
        header("Content-Type: text/html; charset=utf-8");  
        $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("./Data/Ueditor1_4_3/php/config.json")), true);
        $action = $_GET['action'];
        switch ($action) {
            case 'config':
                $result = json_encode($CONFIG);
                break;
                /* 上传图片 */
            case 'uploadimage':
                  import('ORG.Net.UploadFile');
                  $upload = new UploadFile();

                  $upload->autoSub = true;
                  $upload->subType = 'date';
                  $upload->dateFormat = 'Ymd';
                  if ($upload->upload('./Uploads/')){
                     import("ORG.Util.Image");
                     
                      $info = $upload->getUploadFileInfo();
                      //给图片添加水印
                      Image::water('./Uploads/'.$info[0]['savename'],'./Data/ueditor1_4_3/water.png');
                      echo json_encode(array(
                              'url'=>__ROOT__.'/Uploads/'.$info[0]['savename'],
                              'title'=>htmlspecialchars($_POST['pictitle'], ENT_QUOTES),
                              'original'=>    $info[0]['name'],
                              'state'=>'SUCCESS'
                              ));
                      
                  }else{
                      echo json_encode(array(
                              'state'    => $upload->getErrorMsg(),
                              ));
                  }
                  break;    
                /* 上传涂鸦 */
            case 'uploadscrawl':
                /* 上传视频 */
            case 'uploadvideo':
                /* 上传文件 */
            case 'uploadfile':
              $result = include("action_upload.php");
               break;
        }
        
        /* 输出结果 */
        if (isset($_GET["callback"])) {
            if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
                echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
            } else {
                echo json_encode(array(
                        'state'=> 'callback参数不合法'
                ));
            }
        } else {
            echo $result;
        }
    }

}
?>